package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.Com.Category;
import com.itheima.common.Com.Setmeal;
import com.itheima.common.Com.SetmealDishes;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.vo.SetmealDishesVo;
import com.itheima.common.vo.SetmealDto;
import com.itheima.common.vo.SetmealVo;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public SetmealVo getById(String id) {
        //通过前端的传过来的id获取数据库的数据
        SetmealVo byId = setmealMapper.getById(id);

        //new一个对象
        SetmealVo setmealVo=new SetmealVo();


        //复制数据
        BeanUtils.copyProperties(byId, setmealVo);

        //通过id获取查询
        List<SetmealDishesVo> byId1 = setmealMapper.findById(byId.getId());

//        List<SetmealDishesVo> setmealDishes = setmealVo.getSetmealDishes();
//        for (SetmealDishesVo setmealDish : setmealDishes) {
//            setmealDish.setSetmealId(byId.getId());
//        }

        setmealVo.setSetmealDishes(byId1);


//        setmealMapper.findById();

        return setmealVo;

    }

    @Override
    public PageInfo<Setmeal> page(String name, Integer page, Integer pageSize) {
        //获取一个新的pageInfo对象
        PageInfo<Setmeal> pageInfo = new PageInfo<Setmeal>();
        //分页查询的起始页获取公式
        Integer start = (page - 1) * pageSize;
        //将参数带去mapper进行数据库调用返回一个集合
        List<Setmeal> pa = setmealMapper.page(name, start, pageSize);
        //循环遍历集合，将分类id获取出来带到分类表查询分类名
        for (Setmeal setmeal : pa) {
            Category category = categoryMapper.findbyId(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());

        }
        pageInfo.setRecords(pa);

        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        long count = setmealMapper.count(name);
        pageInfo.setTotal(count);
        return pageInfo;

    }

    @Override
    public boolean save(SetmealVo setmealVo) {

        //获取要增加的数据
        Setmeal setmeal = new Setmeal();
        //复制数据
        BeanUtils.copyProperties(setmealVo, setmeal);
        setmeal.setId(IdUtil.getSnowflakeNextIdStr());
        setmealVo.setId(IdUtil.getSnowflakeNextIdStr());
        //将传回来的数据里的集合取出来
        List<SetmealDishesVo> list = setmealVo.getSetmealDishes();

        //遍历取出来的数据
        for (SetmealDishesVo setmealDishesVo : list) {
            setmealDishesVo.setId(IdUtil.getSnowflakeNextIdStr());
            log.info("{}", setmealDishesVo);
            setmealMapper.saveSetmeal(setmealDishesVo);
        }
        //todo 还有bug
        //将数据传下去
        int save = setmealMapper.save(setmeal);
        //接收mapper的结果

        return save > 0;
    }

    @Override
    public boolean update(SetmealVo setmealVo) {

        int update = setmealMapper.update(setmealVo);

        List<SetmealDishes> list = new ArrayList<>();

        List<SetmealDishesVo> setmealDishe = setmealVo.getSetmealDishes();

        BeanUtils.copyProperties(setmealDishe,list );

        for (SetmealDishes setmealDishes : list) {
            setmealDishes.setSetmealId(setmealVo.getId());
            setmealMapper.updatesetmeal(setmealDishe);
        }
        return update > 0;
    }

    @Override
    public boolean delete(String ids) {

        String[] arr = ids.split(",");

        int delete = setmealMapper.delete(arr);

        return delete > 0;
    }

    @Override
    public boolean stop(Integer status, String ids) {


        String[] id = ids.split(",");

        setmealMapper.stop(status, id);

        return false;
    }

    @Override
    public List<SetmealDto> getlist(String categoryId, Integer status) {


        List<SetmealDto> getlist = setmealMapper.getlist(categoryId, status);


        return getlist;
    }

}

